package com.warm.orm.eq.sys.mapper.impl;

import com.easy.query.api4j.client.EasyQuery;
import com.easy.query.solon.annotation.Db;
import com.warm.orm.domain.sys.SysUserRole;
import com.warm.orm.eq.sys.mapper.SysUserRoleMapper;
import org.noear.solon.annotation.Component;

import java.util.List;

@Component
public class SysUserRoleMapperImpl implements SysUserRoleMapper {

    @Db
    private EasyQuery easyQuery;

    /**
     * 批量新增用户角色信息
     *
     * @param userRoleList 用户角色列表
     * @return 结果
     */
    @Override
    public int batchUserRole(List<SysUserRole> userRoleList) {
        return (int) this.easyQuery.insertable(userRoleList).executeRows();
    }

    /**
     * 通过角色ID查询角色使用数量
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Override
    public int countUserRoleByRoleId(Long roleId) {
        // from sys_user_role
        return (int) this.easyQuery.queryable(SysUserRole.class)
                // where role_id = #{roleId}
                .where(t -> t.eq(SysUserRole::getRoleId, roleId))
                // select count(*)
                .count();
    }

    /**
     * 批量删除用户和角色关联
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserRole(Long[] ids) {
        // delete from sys_user_role
        return (int) this.easyQuery.deletable(SysUserRole.class)
                // where role_id in #{ids}
                .where(t -> t.in(SysUserRole::getRoleId, ids))
                .allowDeleteStatement(true) // 允许物理删除
                .executeRows();
    }

    /**
     * 通过用户ID删除用户和角色关联
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int deleteUserRoleByUserId(Long userId) {
        // delete from sys_user_role
        return (int) this.easyQuery.deletable(SysUserRole.class)
                // where user_id = #{userId}
                .where(t -> t.eq(SysUserRole::getUserId, userId))
                .allowDeleteStatement(true) // 允许物理删除
                .executeRows();
    }

    /**
     * 删除用户和角色关联信息
     *
     * @param userRole 用户和角色关联信息
     * @return 结果
     */
    @Override
    public int deleteUserRoleInfo(SysUserRole userRole) {
        // delete from sys_user_role
        return (int) this.easyQuery.deletable(SysUserRole.class)
                // where role_id = #{roleId}
                .where(t -> t.eq(SysUserRole::getRoleId, userRole.getRoleId())
                        // and user_id = #{userId}
                        .eq(SysUserRole::getUserId, userRole.getUserId())
                )
                .allowDeleteStatement(true) // 允许物理删除
                .executeRows();
    }

    /**
     * 批量取消授权用户角色
     *
     * @param roleId  角色ID
     * @param userIds 需要删除的用户数据ID
     * @return 结果
     */
    @Override
    public int deleteUserRoleInfos(Long roleId, Long[] userIds) {
        // delete from sys_user_role
        return (int) this.easyQuery.deletable(SysUserRole.class)
                // where role_id = #{roleId}
                .where(t -> t.eq(SysUserRole::getRoleId, roleId)
                        // and user_id in #{userIds}
                        .in(SysUserRole::getUserId, userIds)
                )
                .allowDeleteStatement(true) // 允许物理删除
                .executeRows();
    }
}
